package com.hcb.tonguetiphealth.manager;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;

public class LctManager {
	private LocationManager manager;
	private Location location = null;
	private double latitude;// 纬度
	private double longitude;// 经度
	private Activity ctx;
	
    private final static String BaiduMapKey = "DF45D1401C31C5328897DE20D783E03930CCA49B";
    //百度经纬度解析成地址
    //http://api.map.baidu.com/geocoder?output=json&location=39.983424,%20116.322987&key=37492c0ee6f924cb5e934fa08c6b1676
   // 返回格式：
//    {
//        "status":"OK",
//        "result":{
//            "location":{
//                "lng":117.308382,
//                "lat":31.804274
//            },
//            "formatted_address":"安徽省合肥市包河区g40",
//            "business":"骆岗",
//            "addressComponent":{
//                "city":"合肥市",
//                "district":"包河区",
//                "province":"安徽省",
//                "street":"g40",
//                "street_number":""
//            },
//            "cityCode":127
//        }
//    }
    private final static String baiduGeocoderUrl = "http://api.map.baidu.com/geocoder?output=json";
    
	public LctManager(Activity ctx) {
		this.ctx = ctx;
	}

	public void getLocation() {
		getLoationByGPS();
	}
	
	/**
	 * 使用gps定位
	 * @return
	 */
	public void getLoationByGPS() {
		   LocationManager loctionManager; 
		      String contextService=Context.LOCATION_SERVICE; 
		      //通过系统服务，取得LocationManager对象 
		      loctionManager=(LocationManager) ctx.getSystemService(contextService); 
		      Criteria criteria = new Criteria(); 
		      criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度 
		      criteria.setAltitudeRequired(false);//不要求海拔 
		      criteria.setBearingRequired(false);//不要求方位 
		      criteria.setCostAllowed(true);//允许有花费 
		      criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗 

		     //从可用的位置提供器中，匹配以上标准的最佳提供器 
		      String provider = loctionManager.getBestProvider(criteria, true); 
		      System.out.println("provider:"+provider);
		      //获得最后一次变化的位置 
		      Location location = loctionManager.getLastKnownLocation(provider);
		      loctionManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, new android.location.LocationListener() {
				
				@Override
				public void onStatusChanged(String provider, int status, Bundle extras) {
					// TODO Auto-generated method stub
				
				}
				
				@Override
				public void onProviderEnabled(String provider) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onProviderDisabled(String provider) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onLocationChanged(Location location) {
					// TODO Auto-generated method stub
					if (location!=null) {
						System.out.println("location"+ location);
						
					}
				}
			});
	}
	
}
